<?php

namespace app\proxyapi\controller;

use app\data\model\LiveAppproxy;
use app\data\model\LiveIntegralRecord;
use app\data\model\LiveProxyList;

class ManageProxy extends BaseProxyController
{
    //代理列表
    public function getProxyList(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset($post['appName'], $post['packageName'], $post['versionName'], $post['versionCode'])) $this->error('缺少参数！');
        $app = LiveAppproxy::mk()->where(['app_name'=>$post['appName'],'app_package'=>$post['packageName'],'deleted'=>0])->find();
        if(empty($app)) $this->error('不存在当前应用！');

        $proxys = LiveProxyList::mk()->field('id as uuid,user_nickname,user_account,create_time')->where(['pid'=>$userId,'deleted'=>0])->select()->toArray();
        $this->success('success',$proxys);
    }
    //新增代理
    public function addProxyUser(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset($post['nickname'], $post['account'], $post['password'], $post['inte_num'])) $this->error('缺少参数！');
        if(empty($post['nickname'])) $this->error('昵称不可为空！');
        if(empty($post['account'])) $this->error('账户名不可为空！');
        if(empty($post['password'])) $this->error('密码不可为空！');
        if(empty($post['inte_num'])) $this->error('充值积分不可为空或为0！');

        //查看积分余额 < 充值积分
        $proxy = LiveProxyList::mk()->where(['id'=>$userId,'deleted'=>0])->find();
        if($proxy['app_id'] == 3) $this->error('当前应用不允许新增代理！');
        if($proxy['integral_balance'] < $post['inte_num']) $this->error('积分余额不足！');
        //账户名是否重复
        if(LiveProxyList::mk()->where(['user_account'=>$post['account']])->count() > 0) $this->error('账户名已存在，请更换！');
        //新增
        $new_uuid = LiveProxyList::mk()->insertGetId([
            'pid'               =>$userId,
            'app_id'            =>$proxy['app_id'],
            'user_nickname'     =>$post['nickname'],
            'user_account'      =>$post['account'],
            'user_password'     =>$post['password'],
            'user_autotype'     =>1,//授权类型 1代理 2贴牌
            'integral_balance'  =>$post['inte_num'],//积分余额
        ]);
        //积分记录-支出
        $new_integral_balance = $proxy['integral_balance'] - $post['inte_num'];
        LiveProxyList::mk()->where(['id'=>$userId,'deleted'=>0])->update(['integral_balance'=>$new_integral_balance]);
        LiveIntegralRecord::mk()->insert([
            'uuid'              =>$userId,
            'app_id'            =>$proxy['app_id'],
            'integral_type'     =>2,//积分类型 1收入 2支出
            'integral_num'      =>-$post['inte_num'],
            'integral_balance'  =>$new_integral_balance,
            'create_uuid'       =>$userId,
            'remarks'=>'新增代理['.$post['nickname'].'：'.$post['account'].']，扣除['.$post['inte_num'].']积分'
        ]);
        //积分记录-收入
        LiveIntegralRecord::mk()->insert([
            'uuid'              =>$new_uuid,
            'app_id'            =>$proxy['app_id'],
            'integral_type'     =>1,//积分类型 1收入 2支出
            'integral_num'      =>$post['inte_num'],
            'integral_balance'  =>$post['inte_num'],
            'create_uuid'       =>$userId,
            'remarks'=>'['.$post['nickname'].'：'.$post['account'].']成为代理，充值['.$post['inte_num'].']积分'
        ]);
        $this->success('新增成功',$new_integral_balance);
    }
    //下发积分
    public function addProxyInte(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset($post['uuid'],$post['inte_num'])) $this->error('缺少参数！');

        //查看积分余额 < 充值积分
        $proxy_setuser = LiveProxyList::mk()->where(['id'=>$userId,'deleted'=>0])->find();
        if($proxy_setuser['app_id'] == 3) $this->error('当前应用不允许下发积分！');
        $proxy_childuser = LiveProxyList::mk()->where(['id'=>$post['uuid'],'deleted'=>0])->find();
        if($proxy_setuser['integral_balance'] < (int)$post['inte_num']) $this->error('积分余额不足！');

        //积分记录-支出
        $set_integral_balance = $proxy_setuser['integral_balance'] - $post['inte_num'];
        LiveProxyList::mk()->where(['id'=>$userId,'deleted'=>0])->update(['integral_balance'=>$set_integral_balance]);
        LiveIntegralRecord::mk()->insert([
            'uuid'              =>$userId,
            'app_id'            =>$proxy_setuser['app_id'],
            'integral_type'     =>2,//积分类型 1收入 2支出
            'integral_num'      =>-$post['inte_num'],
            'integral_balance'  =>$set_integral_balance,
            'create_uuid'       =>$userId,
            'remarks'=>'下发积分给['.$proxy_childuser['user_nickname'].'：'.$proxy_childuser['user_account'].']，扣除['.$post['inte_num'].']积分'
        ]);

        $child_integral_balance = $proxy_childuser['integral_balance'] + $post['inte_num'];
        LiveProxyList::mk()->where(['id'=>$post['uuid'],'deleted'=>0])->update(['integral_balance'=>$child_integral_balance]);
        //积分记录-收入
        LiveIntegralRecord::mk()->insert([
            'uuid'              =>$post['uuid'],
            'app_id'            =>$proxy_childuser['app_id'],
            'integral_type'     =>1,//积分类型 1收入 2支出
            'integral_num'      =>$post['inte_num'],
            'integral_balance'  =>$child_integral_balance,
            'create_uuid'       =>$userId,
            'remarks'=>'['.$proxy_setuser['user_nickname'].'：'.$proxy_setuser['user_account'].']充值到当前账户内['.$post['inte_num'].']积分'
        ]);
        $this->success('充值成功',$set_integral_balance);
    }
}